// pages/home/sousuo/sousuo.js
const http = require('../js/ajax.js')
const api = require('../js/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shang: [],
    remen: [],
    name: "",
    dian: [],
    cunfang: [],
    idaa: [],
    cunzai:true,
    xianshi:true,
    mei:false
    // lishi:[]
  },
  //搜索接口
  soui(e) {
    console.log(e);
    
    // console.log(this.data.name)
    // let cunfang = JSON.parse(JSON.stringify(this.data.cunfang))
    // cunfang.unshift(this.data.name)
    // wx.setStorageSync('cun', cunfang)
    // this.setData({
    //   cunfang: cunfang
    // })
    this.setHis(this.data.name)
    this.setData({
      xianshi:false,
      mei:true
    })
    this.zong();
  },
  //商品总
  zong() {
    let date = {
      "shopId": 18,
      "storeId": 56200,
      "status": "1",
      "keyword": this.data.name,
      "teminal": 2,
      "currentPage": 0,
      "pageSize": 1000
    }
    http(api.splb, "data", date, "POST"
    ).then(res => {
      //console.log("1111", res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          shang: res.data.result.items
        })
      }
    })
  },
  zonga() {
    let date = {
      "shopId": 18,
      "storeId": 56200,
      "status": "1",
      "keyword": this.data.name,
      "teminal": 2,
      "currentPage": 0,
      "pageSize": 1000
    }
    http(api.splb, "data", date, "POST"
    ).then(res => {
      //console.log("1111", res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          remen: res.data.result.items
        })
      }
    })
  },
  //绑定value
  inpu(e) {
    //console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  //点击并存储
  dianji(e) {
    this.setData({
      xianshi: false,
      mei: true
    })
    //console.log(e);
    this.setData({
      dian: e.currentTarget.dataset.goodsname
    })
    try {
      this.data.cunfang.unshift(this.data.dian)
      wx.setStorageSync('cun', this.data.cunfang)
    } catch (e) {

    }
    let date = {
      "shopId": 18,
      "storeId": 56200,
      "status": "1",
      "keyword": this.data.dian,
      "teminal": 2,
      "currentPage": 0,
      "pageSize": 1000
    }
    http(api.splb, "data", date, "POST"
    ).then(res => {
      console.log("222", res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          shang: res.data.result.items
        })
      }
    })
    this.huoqu()
  },
  //点击历史里的
  dianjili(e) {
    this.setData({
      xianshi: false,
      mei: true
    })
    let date = {
      "shopId": 18,
      "storeId": 56200,
      "status": "1",
      "keyword": this.data.cunfang[e.currentTarget.dataset.index],
      "teminal": 2,
      "currentPage": 0,
      "pageSize": 1000
    }
    http(api.splb, "data", date, "POST"
    ).then(res => {
      console.log("222", res)
      if (res.data.status.statusCode === 0) {
        this.setData({
          shang: res.data.result.items
        })
      }
    })
  },
  //获取本地存储
  huoqu() {
    var that = this;
    wx.getStorage({
      key: 'cun',
      success: function (res) {
        console.log(res.data);
        that.setData({
          cunfang: res.data
        })
      },
    })

  },
  //清空
  qingkong() {
    var that = this
    wx.showModal({
      title: '确定要清空历史搜索嘛？',
      content: '',
      success(res) {
        if (res.confirm) {
          //console.log("确定")
          wx.removeStorage({
            key: 'cun',
            success: function (res) {
              console.log(res)
              that.setData({
                cunfang: []
              })
            },

          })
        }
        if (res.cancel) {
          //console.log("取消")
        }
      }
    })
  },
  //数量加
  jia(e) {
    //console.log(e);
    this.setData({
      idaa: e.currentTarget.dataset.id
    })
    console.log(this.data.idaa)
    let data = {
      customerId: 589,
      shopId: 18,
      goodsId: this.data.idaa,
      quantity: 598,
      teminal: 2,
      storeId: 56200
    }
    http(api.slja, "data", data, "POST").then(res => {
      console.log(res)
    })
  },
  //判断是否有历史搜索
  yorm(){
    let value = wx.getStorageSync('cun')
    if(!value){
      this.setData({
        cunzai:false
      })
    }else{
      this.setData({
        cunzai: true
      })
    }
    console.log(this.data.cunzai)
  },
  getHis() {
    let that = this;
      wx.getStorage({
        key: 'cun',
        success: function(res) {
          that.setData({
            cunfang:JSON.parse(res.data)
          })
        },
        fail: function(err) {
          that.setData({
            cunfang: []
          })
        }
      })
  },
  setHis(val) {
    let arr = this.data.cunfang;
    arr.unshift(val);
    let _arr = [...new Set(arr)]
    if(_arr.length > 10) {
      _arr.splice(10,1)
    }
    wx.setStorage({
      key: 'cun',
      data: _arr
    })
    this.setData({
      cunfang: _arr
    })
  },
  // removeHis() {
  //   let that = this;
  //   wx.removeStorage({
  //     key: 'cun',
  //     success: function(res) {
  //       that.setData({
  //         cunfang: []
  //       })
  //       console.log('Success！')
  //     },
  //   })
  // },
  //跳转到购物车
  tgwc(){
    console.log(11111111111111)
    wx.switchTab({
      url: '../../cart/cart',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.zong()
    this.zonga()
    // this.lishi()
    this.huoqu()
    this.yorm()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})